<?php


namespace App\Server;


use App\Http\Controllers\Goods\GoodsFrameworkController;
use App\Models\Cart\SpecGoods;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Request;

class Goods
{
    /**
     * 添加商品
     *
     * @param $param
     * @return \Illuminate\Http\JsonResponse
     */
    public function save($param){
        //
        DB::beginTransaction();
        try {
            $goodsData=[
                'goods_name'=>$param['goods_name'],
                'goods_number'=>$param['goods_name'],
                'type_id'=>$param['goods_name'],
                'brand_id'=>$param['goods_name'],
                'cate_id'=>$param['goods_name'],
            ];
            $goodsRes=\App\Models\Goods\Goods::create($goodsData);
            $goods_id=$goodsRes->id;
            $specGoods=[];
            foreach ($specGoods as $key=>$val){
                $specGoods[]=SpecGoods::create([
                   'goods_id'=>$goods_id,
                   'value_ids'=>$key,
                   'value_names'=>$val['value_names'],
                   'price'=>$val['price'],
                   'store'=>$val['store'],
                ]);
            }
            DB::commit();
            return response()->json(['code'=>200,'msg'=>'添加成功','data'=>[$specGoods]]);
        } catch (\Exception $exception){
            DB::rollBack();
            return response()->json(['code'=>2001,'msg'=>'添加失败']);
        }
    }






}
